﻿using FullApp1.Modules.ModuleName.Models;
using FullApp1.Modules.ModuleName.ViewModels;
using Microsoft.Win32;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FullApp1.Modules.ModuleName.Views
{
    /// <summary>
    /// LicenseDetailView.xaml 的交互逻辑
    /// </summary>
    public partial class LicenseDetailView : UserControl
    {

        public LicenseDetailView()
        {
            InitializeComponent();
        }

        // 身份证正面选择
        private async void SelectFrontIDCard_Click(object sender, RoutedEventArgs e) // 关键：async void
        {
            try
            {
                var openFileDialog = new OpenFileDialog
                {
                    Filter = "图片文件|*.jpg;*.png",
                    Title = "选择身份证正面照片"
                };

                if (openFileDialog.ShowDialog() == true)
                {
                    var imagePath = openFileDialog.FileName;

                    // 调用OCR服务（异步操作）
                    var result = await BaiduOCRService.RecognizeIDCard(imagePath, false);

                    // 更新ViewModel
                    if (ViewModel != null && result != null)
                    {
                        ViewModel.IDCardFront = new BitmapImage(new Uri(imagePath));
                        ViewModel.UpdateLegalPersonInfo(result, false);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        // 身份证反面选择
        private async void SelectBackIDCard_Click(object sender, RoutedEventArgs e) // 关键：async void
        {
            try
            {
                var openFileDialog = new OpenFileDialog
                {
                    Filter = "图片文件|*.jpg;*.png",
                    Title = "选择身份证反面照片"
                };

                if (openFileDialog.ShowDialog() == true)
                {
                    var imagePath = openFileDialog.FileName;

                    // 调用OCR服务（异步操作）
                    var result = await BaiduOCRService.RecognizeIDCard(imagePath,true);

                    // 更新ViewModel
                    if (ViewModel != null && result != null)
                    {
                        ViewModel.IDCardBack = new BitmapImage(new Uri(imagePath));
                        ViewModel.UpdateLegalPersonInfo(result, true);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        // 通过DataContext获取ViewModel的便捷属性
        private LicenseDetailViewModel ViewModel => DataContext as LicenseDetailViewModel;
    }
}
